package cn.shen.common.util;

import cn.shen.common.vo.WebResult;

public class WebResultUtil {
    public static final String SUUCESS_MESSAGE_STRING = "SUCCESS";
    /**
     * 返回成功code=200
     */
    public static final Integer SUUCESS_CODE_200 = 200;
    /**
     * 返回失败code[内容异常]=500
     */
    public static final Integer INTERNAL_SERVER_CODE_500 = 500;

    public static <T> WebResult<T> genSuccessResult(String message, T data) {
        WebResult<T> result = WebResult.newInstance();
        result.setResult(data);
        result.setSuccess(true);
        result.setMessage(message);
        result.setCode(200);
        return result;
    }

    public static <T> WebResult<T> genSuccessResult() {
        WebResult<T> result = WebResult.newInstance();
        result.setResult(null);
        result.setSuccess(true);
        result.setMessage(SUUCESS_MESSAGE_STRING);
        result.setCode(SUUCESS_CODE_200);
        return result;
    }

    public static <T> WebResult<T> genSuccessResult(T data) {
        WebResult<T> result = WebResult.newInstance();
        result.setResult(data);
        result.setSuccess(true);
        result.setMessage(SUUCESS_MESSAGE_STRING);
        result.setCode(SUUCESS_CODE_200);
        return result;
    }

    public static <T> WebResult<T> genResult(int code, boolean success, String message, T data) {
        WebResult<T> result = WebResult.newInstance();
        result.setResult(data);
        result.setSuccess(success);
        result.setMessage(message);
        result.setCode(code);
        return result;
    }

    public static <T> WebResult<T> genErrorResult(String message, T data) {
        WebResult<T> result = WebResult.newInstance();
        result.setResult(data);
        result.setSuccess(false);
        result.setMessage(message);
        result.setCode(INTERNAL_SERVER_CODE_500);
        return result;
    }

    public static <T> WebResult<T> genErrorResult() {
        return genErrorResult("", null);
    }

    public static <T> WebResult<T> genErrorResult(String message) {
        return genErrorResult(message, null);
    }

    public static <T> WebResult<T> genErrorResult(int code, String message) {
        WebResult<T> result = WebResult.newInstance();
        result.setResult(null);
        result.setSuccess(false);
        result.setMessage(message);
        result.setCode(code);
        return result;
    }
}
